var t = getApp(), e = require("../../utils/util.js");

Page({
    data: {
        placeholder: "请输入抽奖活动说明",
        optHidden: !0,
        rules: [ {
            type: "text",
            value: "",
            text: ""
        } ]
    },
    onLoad: function(t) {
        var a = e.getStorage("rules"), s = "请输入抽奖活动说明";
        ((a = a && a.length ? a : [ {
            type: "text",
            value: "",
            text: ""
        } ]).length > 1 || 1 == a.length && "text" == a[0].type && "" != a[0].value) && (s = ""), 
        this.setData({
            rules: a,
            placeholder: s
        });
    },
    setHeight: function() {
        var t = wx.getSystemInfoSync().windowHeight - 55;
        this.setData({
            scrollHeight: t
        });
    },
    onShow: function() {
        this.setHeight();
    },
    onHide: function() {
        wx.setStorage({
            key: "rules",
            data: this.data.rules
        });
    },
    postFormId: function(t) {
        e.postFormId(t.detail.formId);
    },
    delete: function(t) {
        var e = t.currentTarget.dataset.index, a = this.data.rules || [];
        a.splice(e, 1), this.setData({
            rules: a
        });
    },
    inputText: function(t) {
        this.inputTextarea(t, 1);
    },
    inputBlur: function(t) {
        this.setHeight(), this.inputTextarea(t, 2);
    },
    inputTextarea: function(t, e) {
        var a = t.currentTarget.dataset.index, s = this.data.rules;
        s[a] && (s[a].value = t.detail.value, s[a].text = t.detail.value, 2 == e && (s[a].focus = 0), 
        this.setData({
            rules: s
        }));
    },
    inputTitle: function(t) {
        this.inputCard(t, 1);
    },
    inputBody: function(t) {
        this.inputCard(t, 2);
    },
    inputCard: function(t, e) {
        var a = t.currentTarget.dataset.index, s = this.data.rules || [];
        s[a] && (1 == e ? s[a].title = t.detail.value : s[a].body = t.detail.value, this.setData({
            rules: s
        }));
    },
    noDo: function(t) {
        console.log("do nothing ");
        for (var e = this.data.rules, a = 0, s = e.length; a < s; a++) e[a].focus = 0;
        this.setData({
            rules: e
        });
    },
    textFocus: function(t) {
        var e = t.currentTarget.dataset.index;
        console.log(e);
        for (var a = this.data.rules, s = 0, o = a.length; s < o; s++) a[s].focus = s != e ? 0 : 1;
        this.setData({
            rules: a
        }), console.log(a);
    },
    addText: function(t) {
        for (var e = this.data.rules || [], a = e.length, s = 0; s < a; s++) e[s].focus = 0;
        e.length && "text" == e[a - 1].type ? e[a - 1].focus = 1 : e.push({
            type: "text",
            value: "",
            focus: 1
        });
        var o = "请输入抽奖活动说明";
        (e.length > 1 || 1 == e.length && "text" == e[0].type && "" != e[0].value) && (o = ""), 
        this.setData({
            rules: e,
            placeholder: o
        });
    },
    addCard: function(t) {
        var e = this.data.rules || [];
        console.log(e);
        for (var a = e.length, s = 0; s < a; s++) e[s].focus = 0;
        e.push({
            type: "card",
            title: "",
            body: ""
        }), this.setData({
            rules: e,
            optHidden: !0
        });
    },
    addImage: function() {
        this.setData({
            optHidden: !0
        });
        var t = this;
        wx.chooseImage({
            count: 1,
            sizeType: [ "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(e) {
                for (var a = t.data.rules || [], s = a.length, o = 0; o < s; o++) a[o].focus = 0;
                a.push({
                    type: "img",
                    value: ""
                }), console.log(555555), console.log(a), t.setData({
                    rules: a,
                    tempIndex: a.length - 1
                });
                var l = e.tempFilePaths;
                t.uploadImages(l, 0);
            },
            fail: function(e) {
                console.log("fail : ===="), console.log(e);
                var a = t.data.tempIndex || 0, s = t.data.rules;
                console.log(s), console.log(a), s.splice(a, 1), t.setData({
                    rules: s
                });
            }
        });
    },
    openOpt: function() {
        for (var t = this.data.rules, e = t.length, a = 0; a < e; a++) t[a].focus = 0;
        this.setData({
            rules: t,
            optHidden: !1
        });
    },
    closeOpt: function() {
        this.setData({
            optHidden: !0
        });
    },
    uploadImages: function(a, s) {
        var o = this, l = e.getStorage("accessToken");
        if ("" != l) {
            var n = a[0];
            wx.uploadFile({
                url: t.globalData.host + "/file/v1/upload",
                filePath: n,
                name: "file",
                formData: {
                    access_token: l,
                    type: ""
                },
                success: function(t) {
                    var l = t.data, n = JSON.parse(l);
                    if (-500 != t.data.sta) if (0 == n.sta) {
                        var i = n.data.urls, u = o.data.rules;
                        console.log("rules ==== :"), console.log(u), console.log(o.data.tempIndex), console.log(i), 
                        u[o.data.tempIndex].value = i[0], o.setData({
                            rules: u
                        }), a.length - 1 > s ? o.uploadImages(a, s + 1) : e.hideLoading();
                    } else e.hideLoading(), e.showFailedToast(n.msg); else e.login(function() {
                        o.uploadImages(a, s);
                    });
                },
                fail: function(t) {
                    var e = o.data.tempIndex, a = o.data.ruels;
                    a.splice(e, 1), o.setData({
                        rules: a
                    });
                }
            });
        } else o.uploadImages(a, s);
    },
    submitRules: function() {
        e.setStorage("rules", this.data.rules), wx.navigateBack({
            delta: 1
        });
    }
});